﻿// MIT License
//
// Copyright (c) 2021-present zuohuaijun, Daming Co.,Ltd and Contributors
//
// 电话/微信：18020030720 QQ群1：87333204 QQ群2：252381476

using Admin.NET.Application.Const;
using Admin.NET.Core.Service;
using Admin.Net.InformationPlatform.Entity;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Nest;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Admin.NET.Core;
using Admin.Net.InformationPlatform.Entity.Order;
using Admin.Net.InformationPlatform.Service.Order.Dto;
using SjEntityClass;
using Admin.Net.InformationPlatform.Service.Maintenance.Dto;
using Furion.FriendlyException;

namespace Admin.Net.InformationPlatform.Service.Maintenance 
{
    /// <summary>
    /// 运维平台
    /// </summary>
    [ApiDescriptionSettings(groups: "Maintenance", Name = "条码管理", Tag = "条码管理", Order = 200)]

    public class BarCodeService : IDynamicApiController, ITransient
    {
        private readonly SysUserRoleService _sysUserRoleService;
        /// <summary>
        /// 单例数据链接
        /// </summary>
        ISqlSugarClient db;
        /// <summary>
        /// T100数据库
        /// </summary>
        private SqlSugarProvider _T100Db;
        private SqlSugarProvider _SjDb;
        private SqlSugarProvider _plm;
        private SqlSugarProvider _Dilon;


        /// <summary>
        /// 用户信息
        /// </summary>
        TokenUser _User;
        /// <summary>
        /// 成品仓库管理模块
        /// </summary>
        /// <param name="db"></param>
        public BarCodeService(ISqlSugarClient db, SysUserRoleService sysUserRoleService)
        {
            this.db = db;
            this._T100Db = this.db.AsTenant().GetConnection(T100TestArea.ConfigId);
            this._SjDb = this.db.AsTenant().GetConnection(SJDB.ConfigId);
            this._plm = this.db.AsTenant().GetConnection(PLM.ConfigId);
            this._Dilon = this.db.AsTenant().GetConnection(Dilon.ConfigId);
            // 获取 `Jwt` 存储的信息 企业编号与据点信息
            _User = Util.SjCommonUtil.GetT100UserTokenInfo(_T100Db, _Dilon);
            _sysUserRoleService = sysUserRoleService;

        }
        /// <summary>
        ///条码管理
        ///1.工单查询 
        /// </summary>
        /// <returns></returns>
        [HttpPost("/Bar/GetBarCodeList")]
        public async Task<SqlSugarPagedList<BarCodeOutput>> GetCustomerPagedList(BarCodeInput input)
        {
            var output = await _T100Db.Queryable<LsaaucT>()
    .WhereIF(!string.IsNullOrEmpty(input.BarcodeNum), (l) => (l.Lsaauc001.Contains(input.BarcodeNum)))//条码编号
    .WhereIF(!string.IsNullOrEmpty(input.TrackingNum), (l) => (l.Lsaauc003.Contains(input.TrackingNum)))//来源单号
    .WhereIF(!string.IsNullOrEmpty(input.ProductNo), (l) => (l.Lsaauc005.Contains(input.ProductNo)))//料号
    .WhereIF(!string.IsNullOrEmpty(input.BarcodeUsed), (l) => (l.Lsaauc010.Contains(input.BarcodeUsed)))//条码停用
    .WhereIF(!string.IsNullOrEmpty(input.BarcodeState), (l) => (l.Lsaauc012.Contains(input.BarcodeState)))//条码状态
    .OrderBy(l => l.Lsaauc011, OrderByType.Desc) //报备日期排序
    .Select(l => new BarCodeOutput
    {
       Lsaaucent = l.Lsaaucent,
       Lsaaucsite = l.Lsaaucsite,
       BarcodeNum = l.Lsaauc001,
       BarCSJD = l.Lsaauc002,
       TrackingNum = l.Lsaauc003,
       TrackingSeq = l.Lsaauc004,
       ProductNo = l.Lsaauc005,
       StartNum = l.Lsaauc006,
       PackagType = l.Lsaauc007,
       PackagRules = l.Lsaauc008,
       BarMerge = l.Lsaauc009,
       BarcodeUsed = l.Lsaauc010,
       BarTime = l.Lsaauc011,
       BarcodeState = l.Lsaauc012,
       Lsaauc013 = l.Lsaauc013,
       Lsaauc014 = l.Lsaauc014,
       Lsaauc015 = l.Lsaauc015,
       OrderNum = l.Lsaauc016,
       Lsaauc017 = l.Lsaauc017,
       Lsaauc018 = l.Lsaauc018,
       WarehouseLocation = l.Lsaauc019,
       Lsaauc020 = l.Lsaauc020,
       Lsaauc021 = l.Lsaauc021,
       Lsaauc022 = l.Lsaauc022,
       Lsaauc023 = l.Lsaauc023,
       Lsaauc024 = l.Lsaauc024,
       Lsaauc025 = l.Lsaauc025,
       Remark = l.Lsaauc026,
       Lsaauc027 = l.Lsaauc027,
       Lsaauc028 = l.Lsaauc028,
       Lsaauc029 = l.Lsaauc029,
       EProductName = l.Lsaauc030,
       Lsaauc031 = l.Lsaauc031,
       Lsaauc032 = l.Lsaauc032,
       Lsaauc033 = l.Lsaauc033,
       Lsaauc034 = l.Lsaauc034,
       Lsaauc035 = l.Lsaauc035,
       Lsaauc036 = l.Lsaauc036,
       Lsaauc037 = l.Lsaauc037,
       Lsaauc038 = l.Lsaauc038,
       Lsaauc039 = l.Lsaauc039,
       Lsaauc040 = l.Lsaauc040,
       Lsaauc041 = l.Lsaauc041,
       Lsaauc042 = l.Lsaauc042,
       Lsaauc043 = l.Lsaauc043,
       Lsaauc044 = l.Lsaauc044,
       Lsaauc045 = l.Lsaauc045,

    }).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }

        /// <summary>
        /// （工单）更改条码状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Bar/UpdateBarcodeState")]
        public async Task<string> UpdateComputerRoom(BarCodeUpdate input)
        {
                //判断单据存在
                int cnt = await _T100Db.Queryable<LsaaucT>()
                    .Where(a => a.Lsaauc001 == input.BarcodeNum)
                    .Select(a => a.Lsaauc001)
                    .CountAsync();
                if (cnt == 0)
                {
                    throw Oops.Oh("任务不存在，驳回！");
                }
                else
                {
                    //提交
                    try
                    {
                        _SjDb.Ado.BeginTran();
                        int count = await _T100Db.Updateable<LsaaucT>()
                            .SetColumns(a => a.Lsaauc012 == input.BarcodeState)
                            .Where(a => a.Lsaauc001 == input.BarcodeNum)
                            .ExecuteCommandAsync();

                        if (count == 0)
                        {
                            throw Oops.Oh("修改失败！");
                        }
                        _SjDb.Ado.CommitTran();
                        return "修改成功！";
                    }
                    catch (Exception e)
                    {
                        _SjDb.Ado.RollbackTran();
                        throw Oops.Oh($"异常：{e.Message}");
                    }
                }
           
        }
        /// <summary>
        ///条码管理
        ///1.采购单查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("/Bar/GetBarCodePurList")]
        public async Task<SqlSugarPagedList<BarCodePurOurput>> GetBarCodePurList(BarCodePurInput input)
        {
            var output = await _T100Db.Queryable<CbcaaucT>()
    .WhereIF(!string.IsNullOrEmpty(input.BarcodeNum), (l) => (l.Cbcaauc001.Contains(input.BarcodeNum)))//条码
    .WhereIF(!string.IsNullOrEmpty(input.TrackingNum), (l) => (l.Cbcaauc010.Contains(input.TrackingNum)))//来源单号
    .WhereIF(!string.IsNullOrEmpty(input.TrackingSeq), (l) => (l.Cbcaauc011.Contains(input.TrackingSeq)))//来源单号项次
    .OrderBy(l => l.Cbcaauc008, OrderByType.Desc) //日期排序
    .Select(l => new BarCodePurOurput
    {
        Cbcaaucent = l.Cbcaaucent,
        Cbcaaucsite = l.Cbcaaucsite,
        BarcodeNum = l.Cbcaauc001,
        BarcodeType = l.Cbcaauc002,
        ProductNo = l.Cbcaauc003,
        ProductName = l.Cbcaauc004,
        ProductSpecifications = l.Cbcaauc005,
        Unit = l.Cbcaauc006,
        BarcodeUser = l.Cbcaauc007,
        BarcodeTime = l.Cbcaauc008,
        BarcodeState = l.Cbcaauc009,
        TrackingNum = l.Cbcaauc010,
        TrackingSeq = l.Cbcaauc011,
        Cbcaauc012 = l.Cbcaauc012,
        Cbcaauc013 = l.Cbcaauc013,
        BarcodeCount = l.Cbcaauc014,
        Cbcaauc015 = l.Cbcaauc015,
        BarcodeYXState = l.Cbcaauc016,
        Cbcaauc017 = l.Cbcaauc017,
        Cbcaauc018 = l.Cbcaauc018,
        BarcodeRemark = l.Cbcaauc019,
        Cbcaauc020 = l.Cbcaauc020,
        Cbcaauc021 = l.Cbcaauc021,
        Cbcaauc022 = l.Cbcaauc022,
        Cbcaauc023 = l.Cbcaauc023,
        Cbcaauc024 = l.Cbcaauc024,
        Cbcaauc025 = l.Cbcaauc025,
        Cbcaauc026 = l.Cbcaauc026,
        Cbcaauc027 = l.Cbcaauc027,
        Cbcaauc028 = l.Cbcaauc028,
       

    }).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }

        /// <summary>
        /// （采购单）更改条码状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Bar/UpdateBarcodePurState")]
        public async Task<string> UpdateBarcodePurState(BarCodeUpdate input)
        {
            //判断单据存在
            int cnt = await _T100Db.Queryable<CbcaaucT>()
                .Where(a => a.Cbcaauc001 == input.BarcodeNum)
                .Select(a => a.Cbcaauc001)
                .CountAsync();
            if (cnt == 0)
            {
                throw Oops.Oh("任务不存在，驳回！");
            }
            else
            {
                //提交
                try
                {
                    _SjDb.Ado.BeginTran();
                    int count = await _T100Db.Updateable<CbcaaucT>()
                        .SetColumns(a => a.Cbcaauc009 == input.BarcodeState)
                        .Where(a => a.Cbcaauc001 == input.BarcodeNum)
                        .ExecuteCommandAsync();

                    if (count == 0)
                    {
                        throw Oops.Oh("修改失败！");
                    }
                    _SjDb.Ado.CommitTran();
                    return "修改成功！";
                }
                catch (Exception e)
                {
                    _SjDb.Ado.RollbackTran();
                    throw Oops.Oh($"异常：{e.Message}");
                }
            }

        }
    }
}